package io.renren.task;

import io.renren.entity.SysUserEntity;
import io.renren.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 测试定时任务(演示Demo，可删除)
 * 
 * testTask为spring bean的名称
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月30日 下午1:34:24
 */
@Component("testTask")
public class TestTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private static List list = new ArrayList();
    private static int count = 0;
	@Autowired
	private SysUserService sysUserService;
	
	public void test(String params) throws InterruptedException{
		logger.info("我是带参数的test方法，正在被执行，参数为：" + params);
		
		try {
			Thread.sleep(1000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		SysUserEntity user = sysUserService.queryObject(1L);
		 
	     
		//System.out.println(ToStringBuilder.reflectionToString(user));
		
	}
	
	
	public void test2(){
		logger.info("我是不带参数的test2方法，正在被执行");
	}
	
	public static void main(String[] args) throws InterruptedException {
		System.out.println("申请前的可用内存 = "+getFreeMemory());
        while(true){
             list.add(new byte[1024*1024]);//用实例变量申请1M内存，当方法执行完毕时，这个static的变量是不会被释放
             count++;
             if (count % 100 == 0) {
            	 System.out.println("申请前的最大内存 = "+Runtime.getRuntime().maxMemory()/ (1024 * 1024));
            	 System.out.println("申请前的总内存 = "+Runtime.getRuntime().totalMemory()/ (1024 * 1024));
                 System.out.println("当前list.size()="+list.size()+",可用内存 = "+getFreeMemory());
                 Thread.sleep(500);
             }
        }
	
	}
	     
	    public static long getFreeMemory() {
	    	 
	          return Runtime.getRuntime().freeMemory() / (1024 * 1024);
	         }
	 
}
